package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.BookOrder;
import com.xmy.cultivate.entity.BookStudent;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.request.BookGrantInfoRs;
import com.xmy.cultivate.entity.request.BookGrantRs;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 订购学生 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-01-04
 */
public interface BookStudentMapper extends BaseMapper<BookStudent> {

//修改
    @Insert({"<script>",
            "INSERT INTO book_student(year_part,quarter_num,school_id,student_id,subjects_id,class_type_id,year_class_id,lesson_type,grade_id,teacher_id,created_id,created_at) ",
            "SELECT",
            "t01.year_part,",
            "t01.`quarter_num`,",
            "t01.school_id,",
            "t01.student_id,",
            "t01.subjects_id,",
            "t01.class_type_id,",
            "t01.year_class_id,",
            "t01.lesson_type,",
            "t01.grade_id,",
            "t01.teacher_id,",
            "'${bookOrder.createdId}',",
            "'${bookOrder.createdAt}'",
            "FROM",
            "(",
            "SELECT",
            "t1.id,",
            "t1.school_id,",
            "t1.student_id,",
            "t1.subjects_id,",
            "t1.lesson_type,",
            "t1.year_part,",
            "t1.quarter_num,",
            "t1.grade_id,",
            "t2.year_class_id,",
            "t2.course_type_id AS class_type_id,",
            "t2.teacher_id",
            "FROM",
            "student_grade AS t1",
            "INNER JOIN grade AS t2 ON t1.grade_id = t2.id ",
            "WHERE",
            "t1.year_part = ${bookOrder.yearPart} ",
            "AND t1.quarter_num = ${bookOrder.quarterNum} ",
            "AND t2.subjects_id =  ${bookOrder.subjectsId}",
            "AND t2.course_type_id = ${bookOrder.classTypeId} ",
            "AND  t2.school_id = #{schoolId} ",
//            "<if test='schoolId !=null and schoolId!=&apos;&apos;'>",
//            "AND  t2.school_id = #{schoolId} ",
//            "</if>",
            "AND t1.`status` = 1 ",
            "AND t1.reading_status IN ( 0, 1 ) ",
            "AND t1.lesson_type = ${bookOrder.lessonType} ",
            "GROUP BY",
            "student_id,",
            "subjects_id ",
            ") AS t01",
            "LEFT JOIN book_student AS t03 ON t01.year_part = t03.year_part ",
            "AND t01.quarter_num = t03.quarter_num ",
            "AND t01.student_id = t03.student_id ",
            "AND t01.subjects_id = t03.subjects_id ",
            "AND t01.lesson_type = t03.lesson_type ",
            "AND t01.class_type_id = t03.class_type_id ",
            "AND t01.year_class_id = t03.year_class_id ",
            //"AND t01.school_id= t03.school_id",
            "WHERE",
            "t03.id IS NULL ",
            "</script>"})
    public Integer insertion(@Param("bookOrder") BookOrder bookOrder,@Param("schoolId") String schoolId);

    @Insert({"<script>",
            "INSERT INTO book_student(year_part,quarter_num,school_id,book_text_id,book_text_type,student_id,subjects_id,class_type_id,year_class_id,lesson_num,lesson_type,grade_id,teacher_id,created_id,created_at) ",

            "<when test='bookOrder.subscribeType == 1 '>",
                "WITH subscribe_student_tb AS (",
                    "SELECT * FROM (",
                        "SELECT ",
                        "t01.id,",
                        "t01.student_name,",
                        "t01.student_id,",
                        "t01.class_type_id,",
                        "t01.year_class_id,",
                        "t01.school_id,",
                        "lowest_shutnum,",
                        "t02.subjects_id,",
                        "IFNULL( t02.right_num, 0 ) AS right_num,",
                        "IFNULL( t02.amend_num, 0 ) AS amend_num,",
                        "CASE WHEN lowest_shutnum &gt; 0 THEN ROUND( IFNULL( right_num / lowest_shutnum, 0 ) * 100, 2 ) ELSE 0 END AS rate ",
                        "FROM ",
                        "<if test=' bookOrder.subjectsId == 2'>",
                            "math_db.per_student_account AS t01",
                        "</if>",
                        "<if test=' bookOrder.subjectsId == 3'>",
                            "physics_db.per_student_account AS t01",
                        "</if>",
                        "INNER JOIN (",
                            "SELECT * FROM ",
                            "<if test=' bookOrder.subjectsId == 2'>",
                                "math_db.cha_plan",
                            "</if>",
                            "<if test=' bookOrder.subjectsId == 3'>",
                                "physics_db.cha_plan",
                            "</if>",
                            "WHERE",
                            "year_part = ${bookOrder.yearPart} ",
                            "AND quarter_num = ${bookOrder.quarterNum}",
                            "AND task_category_id = 1 ",
                            "AND deleted = 0 ",
                            "AND lesson_num = ${bookOrder.beforeLessonNum}",
                            "AND is_first = 1 ",
                        ") AS t02 ON t01.student_id = t02.student_id ",
                        "AND t01.class_type_id = t02.class_type_id ",
                        "AND t01.year_class_id = t02.year_class_id ",
                    ") AS t ",
                "WHERE rate &lt; ${rade} )",
            "</when>",

            "SELECT",
            "t01.year_part,",
            "t01.`quarter_num`,",
            "t01.school_id,",
            "${bookOrder.bookTextId},",
            "${bookOrder.bookTextType},",
            "t01.student_id,",
            "t01.subjects_id,",
            "t01.class_type_id,",
            "t01.year_class_id,",
            "${bookOrder.lessonNum},",
            "t01.lesson_type,",
            "t01.grade_id,",
            "t01.teacher_id,",
            "'${bookOrder.createdId}',",
            "'${bookOrder.createdAt}'",
            "FROM",
            "(",
                "SELECT",
                "t1.id,",
                "t1.school_id,",
                "t1.student_id,",
                "t1.subjects_id,",
                "t1.lesson_type,",
                "t1.year_part,",
                "t1.quarter_num,",
                "t1.grade_id,",
                "t2.year_class_id,",
                "t2.course_type_id AS class_type_id,",
                "t2.teacher_id",
                "FROM",
                "student_grade AS t1",
                "INNER JOIN grade AS t2 ON t1.grade_id = t2.id ",
                "INNER JOIN book_text as t3 on t3.id = ${bookOrder.bookTextId}",
                "INNER JOIN book_text_year_class as t4 on t3.id = t4.book_text_id and t2.year_class_id = t4.year_class_id and t2.subjects_id = t4.subjects_id",
                "<when test='bookOrder.subscribeType == 1 '>",
                    "INNER JOIN subscribe_student_tb AS t7 ON t7.student_id = t1.student_id AND t7.subjects_id = t1.subjects_id AND t7.class_type_id = t2.course_type_id ",
                "</when>",
                "WHERE",
                "t1.year_part = ${bookOrder.yearPart} ",
                "AND t1.quarter_num = ${bookOrder.quarterNum} ",
                "AND t1.school_id in (${schoolIdStr})",
                "AND t2.subjects_id =  ${bookOrder.subjectsId}",
                "AND t2.course_type_id = ${bookOrder.classTypeId} ",
                "AND t1.`status` = 1 ",
                "AND t1.reading_status IN ( 0, 1 ) ",
                "AND t1.lesson_type = ${bookOrder.lessonType} ",
                "<when test='bookOrder.subscribeType == 1 '>",
                    "AND t7.id IS NOT NULL ",
                "</when>",
                "GROUP BY",
                "<when test='bookOrder.bookTextType ==1 '>",
                    "t1.student_id,",
                "</when>",
                "<when test='bookOrder.bookTextType ==2 '>",
                    "t2.teacher_id,",
                "</when>",
                "t2.course_type_id,",
                "t2.year_class_id,",
                "t2.subjects_id, ",
                "t2.school_id ",
            ") AS t01",
            "LEFT JOIN book_student AS t03 ON t01.year_part = t03.year_part ",
            "AND t01.quarter_num = t03.quarter_num ",
            "<when test='bookOrder.bookTextType == 1 '>",
                "AND t01.student_id = t03.student_id ",
            "</when>",
            "<when test='bookOrder.bookTextType == 2 '>",
                "AND t01.teacher_id = t03.teacher_id ",
            "</when>",
            "<when test='bookOrder.subscribeType == 1 '>",
                "AND t03.lesson_num = ${bookOrder.lessonNum}",
            "</when>",
            "AND t01.subjects_id = t03.subjects_id ",
            "AND t01.lesson_type = t03.lesson_type ",
            "AND t01.class_type_id = t03.class_type_id ",
            "AND t01.year_class_id = t03.year_class_id ",
            "and t03.book_text_id = ${bookOrder.bookTextId} ",
            "and t03.book_text_type = ${bookOrder.bookTextType} ",

            "LEFT JOIN book_exclude_subscription AS t04 ON t01.year_part = t04.year_part ",
            "AND t01.quarter_num = t04.quarter_num ",
            "AND t01.subjects_id = t04.subjects_id ",
            "AND t01.lesson_type = t04.lesson_type ",
            "AND t01.class_type_id = t04.class_type_id ",
            "AND t01.year_class_id = t04.year_class_id ",
            "AND t01.school_id = t04.school_id ",
            "AND t04.deleted = 0  ",
            "AND t04.book_text_id = ${bookOrder.bookTextId}",
            //1学生用书，2教师用书
            "AND t04.book_text_type = ${bookOrder.bookTextType} ",
            "<when test='bookOrder.bookTextType == 1 '>",
                "AND t01.student_id = t04.student_id ",
            "</when>",
            "<when test='bookOrder.bookTextType == 2 '>",
                "AND t01.teacher_id = t04.teacher_id ",
            "</when>",
            "<when test='bookOrder.subscribeType == 1 '>",
                "AND t04.lesson_num = ${bookOrder.lessonNum} ",
            "</when>",

            "WHERE",
            "t03.id IS NULL ",
            "AND t04.id IS NULL ",
            "</script>"})
    public Integer batchAdd(@Param("bookOrder") BookOrder bookOrder,@Param("schoolIdStr") String schoolIdStr,@Param("rade") Integer rade);

    @Select({"<script>",
            "SELECT",
                    "id,",
                    "year_part,",
                    "quarter_num,",
                    "school_id,",
                    "subjects_id,",
                    "student_id,",
                    "year_class_id,",
                    "status,",
                    "getStudentNameForId ( student_id ) AS student_name,",
                    "getSchoolName ( school_id ) AS school_name,",
                    "getDictName ( year_class_id, 14 ) AS year_class_name,",
                    "getSubjectsName ( subjects_id ) AS subjects_name,",
                    "getCourseTypeName ( class_type_id ) AS class_type_name ",
                    "FROM",
                    "book_student ",
                    "WHERE deleted = 0",
                    "and year_part = ${yearPart} ",
                    "AND quarter_num = ${quarterNum} ",
                    "<if test='schoolId !=null'>",
                        "AND school_id = ${schoolId} ",
                    "</if>",
                    "AND subjects_id = ${subjectsId} ",
                    "AND student_id = ${studentId}",
                    "<if test='yearClassId !=null'>",
                        "AND year_class_id = ${yearClassId}",
                    "</if>",
                    "<if test='classTypeId !=null'>",
                        "AND class_type_id = ${classTypeId}",
                    "</if>",
                    "AND lesson_type = ${lessonType}",
                    "limit 1",
            "</script>"})
    public BookStudent getBookOneForStudentId(@Param("yearPart")Integer yearPart,
                                                 @Param("quarterNum")Integer quarterNum,
                                                 @Param("schoolId")Long schoolId,
                                                 @Param("subjectsId")Long subjectsId,
                                                 @Param("studentId")Long studentId,
                                                 @Param("yearClassId")Integer yearClassId,
                                                 @Param("classTypeId")Long classTypeId,
                                                 @Param("lessonType")Integer lessonType
                                                 );


    @Select({"<script>",
            "SELECT",
                    "count(t1.id) as count",
                    "FROM",
                    "book_student AS t1",
                    "INNER JOIN student_grade AS t2 ON t1.year_part = t2.year_part ",
                    "AND t1.quarter_num = t2.quarter_num ",
                    "AND t1.school_id = t2.school_id ",
                    "AND t1.student_id = t2.student_id ",
                    "AND t1.subjects_id = t2.subjects_id ",
                    "AND t1.lesson_type = t2.lesson_type ",
                    "AND t2.is_last = 1 ",
                    "AND t2.reading_status IN ( 0, 1 ) ",
                    "AND t1.deleted = t2.deleted",
                    "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
                    "AND t3.deleted = 0 ",
                    "WHERE",
                    "t1.`status` = ${status} ",
                    "and t1.year_part = ${yearPart}",
                    "and t1.quarter_num = ${quarterNum}",
                    "AND t1.deleted = 0 ",
                    "AND t3.teacher_id = ${teacherId}",
                    "AND t1.school_id = ${schoolId}",
                    "AND t1.subjects_id = ${subjectsId}",
            "</script>"})
    public Long getNeedGrantCount(@Param("yearPart")Integer yearPart,
                                  @Param("quarterNum")Integer quarterNum,
                                  @Param("schoolId")Long schoolId,
                                  @Param("subjectsId")Long subjectsId,
                                  @Param("teacherId")Long teacherId,
                                  @Param("status")Integer status);

    /**
     *
     * 教师发放
     *
     * @param yearPart
     * @param quarterNum
     * @param dateTimeNow
     * @param status 教师发放：3，学生发放：4
     * @param laterStatus 教师发放：4，学生发放：5
     * @return
     */
    @Insert({"<script>",
            "INSERT INTO book_grant_info ( year_part,quarter_num,book_student_id, grant_id,head_img, `status`,created_id, created_at ) SELECT",
                    "t1.year_part,",
                    "t1.quarter_num,",
                    "t1.id AS book_student_id,",
                    "t3.teacher_id AS grade_teacher_id,",
                    "#{imgOss} AS head_img,",
                    "${laterStatus} AS STATUS,",
                    "${adminId} AS created_id,",
                    "#{dateTimeNow} AS created_at ",
                    "FROM",
                    "book_student AS t1",
                    "INNER JOIN student_grade AS t2 ON t1.year_part = t2.year_part ",
                    "AND t1.quarter_num = t2.quarter_num ",
                    "AND t1.school_id = t2.school_id ",
                    "AND t1.student_id = t2.student_id ",
                    "AND t1.subjects_id = t2.subjects_id ",
                    "AND t1.year_class_id = t2.year_class_id ",
                    "AND t1.lesson_type = t2.lesson_type ",
                    "AND t2.is_last = 1 ",
                    "AND t2.reading_status IN ( 0, 1 ) ",
                    "AND t1.deleted = t2.deleted",
                    "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
                    "AND t3.deleted = t1.deleted",
                    "WHERE",
                    "t1.`status` = ${status} ",
                    "and t1.year_part = ${bookGrantRs.yearPart}",
                    "and t1.quarter_num = ${bookGrantRs.quarterNum}",
                    "AND t1.deleted = 0 ",
                    "AND t3.teacher_id = ${bookGrantRs.teacherId}",
                    "AND t1.school_id = ${bookGrantRs.schoolId}",
                    //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
                    "AND ",
                    "<foreach collection='bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
                        "( ",
                        "t1.year_class_id = ${bookStudent.yearClassId}",
                        "AND t1.class_type_id = ${bookStudent.classTypeId} ",
                        "AND t1.subjects_id = ${bookStudent.subjectsId} ",
                        "AND t1.book_text_id = ${bookStudent.bookTextId} ",
                         // 1学生用书，2教师用书
                        "AND t1.book_text_type = ${bookStudent.bookTextType} ",
                        "<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
                            "AND t2.grade_id = ${bookStudent.gradeId} ",
                        "</when>",
                        "<when test='bookStudent.lessonNum !=null and bookStudent.lessonNum != &apos;&apos;'>",
                            "AND t1.lesson_num = ${bookStudent.lessonNum} ",
                        "</when>",
                        ") ",
                    "</foreach>",
    "</script>"})
    public Integer addDataBookGrant(@Param("yearPart") Integer yearPart,
                                    @Param("quarterNum") Integer quarterNum,
                                    @Param("adminId") Long adminId,
                                    @Param("dateTimeNow") String dateTimeNow,
                                    @Param("status") Integer status,
                                    @Param("laterStatus") Integer laterStatus,
                                    @Param("imgOss") String imgOss,
                                    @Param("bookGrantRs") BookGrantRs bookGrantRs,
                                    @Param("bookGrantInfoRsList") List<BookGrantInfoRs> BookGrantInfoRsList
    );

    /**
     * 教师用书发放
     * @param yearPart
     * @param quarterNum
     * @param adminId
     * @param dateTimeNow
     * @param status
     * @param laterStatus
     * @param imgOss
     * @param bookGrantRs
     * @return
     */
    @Insert({"<script>",
            "INSERT INTO book_grant_info ( year_part,quarter_num,book_student_id, grant_id,head_img, `status`,created_id, created_at ) SELECT",
            "t1.year_part,",
            "t1.quarter_num,",
            "t1.id AS book_student_id,",
            "t3.teacher_id AS grade_teacher_id,",
            "#{imgOss} AS head_img,",
            "${laterStatus} AS STATUS,",
            "${adminId} AS created_id,",
            "#{dateTimeNow} AS created_at ",
            "FROM",
            "book_student AS t1",
            "INNER JOIN (",
                  "SELECT",
                    "sg01.*,",
                    "g01.teacher_id,",
                    "g01.course_type_id AS class_type_id ",
                  "FROM",
                    "student_grade AS sg01",
                    "INNER JOIN grade AS g01 ON sg01.grade_id = g01.id ",
                  "WHERE",
                    "sg01.year_part = ${bookGrantRs.yearPart} ",
                    "AND sg01.quarter_num = ${bookGrantRs.quarterNum} ",
                    "AND g01.lesson_type = 1 ",
                    "AND g01.deleted = 0 ",
                    "AND sg01.is_last = 1 ",
                    "AND sg01.reading_status IN ( 0, 1 ) ",
                  "GROUP BY",
                    "g01.teacher_id,",
                    "g01.school_id,",
                    "g01.year_class_id,",
                    "g01.course_type_id,",
                    "g01.subjects_id ",
                  ") AS t2 ON t1.year_part = t2.year_part ",
            "AND t1.quarter_num = t2.quarter_num ",
            "AND t1.school_id = t2.school_id ",
            "AND t1.teacher_id = t2.teacher_id ",
            "AND t1.subjects_id = t2.subjects_id ",
            "AND t1.year_class_id = t2.year_class_id ",
            "AND t1.class_type_id = t2.class_type_id  ",
            "AND t1.lesson_type = t2.lesson_type ",
            "AND t1.deleted = t2.deleted",
            "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
            "AND t3.deleted = t1.deleted",
            "LEFT JOIN book_grant_info as t4 on t1.id = t4.book_student_id and t4.`status` = ${laterStatus}",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${bookGrantRs.yearPart}",
            "and t1.quarter_num = ${bookGrantRs.quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t3.teacher_id = ${bookGrantRs.teacherId}",
            "AND t1.school_id = ${bookGrantRs.schoolId}",
            "and t4.id is null",
            //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
            "AND ",
            "<foreach collection='bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
            "( ",
            "t1.year_class_id = ${bookStudent.yearClassId}",
            "AND t1.class_type_id = ${bookStudent.classTypeId} ",
            "AND t1.subjects_id = ${bookStudent.subjectsId} ",
            "AND t1.book_text_id = ${bookStudent.bookTextId} ",
            // 1学生用书，2教师用书
            "AND t1.book_text_type = ${bookStudent.bookTextType} ",
            "<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
            "AND t2.grade_id = ${bookStudent.gradeId} ",
            "</when>",
            ") ",
            "</foreach>",
            "</script>"})
    public Integer addDataBookGrantForTeacher(@Param("yearPart") Integer yearPart,
                                    @Param("quarterNum") Integer quarterNum,
                                    @Param("adminId") Long adminId,
                                    @Param("dateTimeNow") String dateTimeNow,
                                    @Param("status") Integer status,
                                    @Param("laterStatus") Integer laterStatus,
                                    @Param("imgOss") String imgOss,
                                    @Param("bookGrantRs") BookGrantRs bookGrantRs,
                                    @Param("bookGrantInfoRsList") List<BookGrantInfoRs> BookGrantInfoRsList
    );



    /**
     * 按老师发放书本
     *
     * @param yearPart
     * @param quarterNum
     * @param status      教师发放：3，学生发放：4
     * @param laterStatus 教师发放：4，学生发放：5
     * @return
     */
    @Update({"<script>",
            "UPDATE book_student AS t1",
                    "INNER JOIN student_grade AS t2 ON t1.year_part = t2.year_part ",
                    "AND t1.quarter_num = t2.quarter_num ",
                    "AND t1.school_id = t2.school_id ",
                    "AND t1.student_id = t2.student_id ",
                    "AND t1.subjects_id = t2.subjects_id ",
                    "AND t1.lesson_type = t2.lesson_type ",
                    "AND t1.year_class_id = t2.year_class_id ",
                    "AND t2.is_last = 1 ",
                    "AND t2.reading_status IN ( 0, 1 ) ",
                    "AND t1.deleted = t2.deleted",
                    "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
                    "AND t3.deleted = t1.deleted ",
                    "SET t1.`status` = ${laterStatus} ",
                    "WHERE",
                    "t1.`status` = ${status} ",
                    "and t1.year_part = ${bookGrantRs.yearPart}",
                    "and t1.quarter_num = ${bookGrantRs.quarterNum}",
                    "AND t1.deleted = 0 ",
                    "AND t3.teacher_id = ${bookGrantRs.teacherId}",
                    "AND t1.school_id = ${bookGrantRs.schoolId}",
                    //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
                    "AND ",
                    "<foreach collection='bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
                        "( ",
                        "t1.year_class_id = ${bookStudent.yearClassId}",
                        "AND t1.class_type_id = ${bookStudent.classTypeId} ",
                        "AND t1.book_text_id = ${bookStudent.bookTextId} ",
                        // 1学生用书，2教师用书
                        "AND t1.book_text_type = ${bookStudent.bookTextType} ",
                        "AND t1.subjects_id = ${bookStudent.subjectsId} ",
                        "<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
                            "AND t2.grade_id = ${bookStudent.gradeId} ",
                        "</when>",
                        "<when test='bookStudent.lessonNum !=null and bookStudent.lessonNum != &apos;&apos;'>",
                            "AND t1.lesson_num = ${bookStudent.lessonNum} ",
                        "</when>",
                        ") ",
                    "</foreach>",
            "</script>"})
    public Integer grantBook(@Param("yearPart") Integer yearPart,
                             @Param("quarterNum") Integer quarterNum,
                             @Param("adminId") Long adminId,
                             @Param("status") Integer status,
                             @Param("laterStatus") Integer laterStatus,
                             @Param("bookGrantRs") BookGrantRs bookGrantRs,
                             @Param("bookGrantInfoRsList") List<BookGrantInfoRs> BookGrantInfoRsList
    );


    /**
     * 教师用书
     * @param yearPart
     * @param quarterNum
     * @param adminId
     * @param status
     * @param laterStatus
     * @param bookGrantRs
     * @return
     */
    @Update({"<script>",
            "UPDATE book_student AS t1",
            "INNER JOIN (",
                "SELECT",
                "sg01.*,",
                "g01.teacher_id,",
                "g01.course_type_id AS class_type_id ",
                "FROM",
                "student_grade AS sg01",
                "INNER JOIN grade AS g01 ON sg01.grade_id = g01.id ",
                "WHERE",
                "sg01.year_part = ${bookGrantRs.yearPart} ",
                "AND sg01.quarter_num = ${bookGrantRs.quarterNum} ",
                "AND g01.lesson_type = 1 ",
                "AND g01.deleted = 0 ",
                "AND sg01.is_last = 1 ",
                "AND sg01.reading_status IN ( 0, 1 ) ",
                "GROUP BY",
                "g01.teacher_id,",
                "g01.school_id,",
                "g01.year_class_id,",
                "g01.course_type_id,",
                "g01.subjects_id ",
            ")",
            "AS t2 ON t1.year_part = t2.year_part ",
            "AND t1.quarter_num = t2.quarter_num ",
            "AND t1.school_id = t2.school_id ",
            "AND t1.teacher_id = t2.teacher_id ",
            "AND t1.subjects_id = t2.subjects_id ",
            "AND t1.class_type_id = t2.class_type_id  ",
            "AND t1.year_class_id = t2.year_class_id ",
            "AND t1.lesson_type = t2.lesson_type ",
            "AND t1.deleted = t2.deleted",
            "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
            "AND t3.deleted = t1.deleted ",
            //"LEFT JOIN book_grant_info as t4 on t1.id = t4.book_student_id and t4.`status` = ${laterStatus}",
            "SET t1.`status` = ${laterStatus} ",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${bookGrantRs.yearPart}",
            "and t1.quarter_num = ${bookGrantRs.quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t3.teacher_id = ${bookGrantRs.teacherId}",
            "AND t1.school_id = ${bookGrantRs.schoolId}",
            //"and t4.id is null",
            //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
            "AND ",
            "<foreach collection='bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
                "( ",
                "t1.year_class_id = ${bookStudent.yearClassId}",
                "AND t1.class_type_id = ${bookStudent.classTypeId} ",
                "AND t1.book_text_id = ${bookStudent.bookTextId} ",
                // 1学生用书，2教师用书
                "AND t1.book_text_type = ${bookStudent.bookTextType} ",
                "AND t1.subjects_id = ${bookStudent.subjectsId} ",
                //"<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
                    //"AND t2.grade_id = ${bookStudent.gradeId} ",
                //"</when>",
                ") ",
            "</foreach>",
            "</script>"})
    public Integer grantBookForTeacher(@Param("yearPart") Integer yearPart,
                             @Param("quarterNum") Integer quarterNum,
                             @Param("adminId") Long adminId,
                             @Param("status") Integer status,
                             @Param("laterStatus") Integer laterStatus,
                             @Param("bookGrantRs") BookGrantRs bookGrantRs,
                             @Param("bookGrantInfoRsList") List<BookGrantInfoRs> BookGrantInfoRsList
    );



    @Select({"<script>",
            "SELECT",
            "count(t1.id) as count",
            "FROM",
            "book_student_paper AS t1",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${yearPart}",
            "and t1.quarter_num = ${quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t1.teacher_id = ${teacherId}",
            "AND t1.school_id = ${schoolId}",
            "AND t1.subjects_id = ${subjectsId}",
            "</script>"})
    public Long getNeedGrantPaperCount(@Param("yearPart") Integer yearPart,
                                       @Param("quarterNum") Integer quarterNum,
                                       @Param("schoolId") Long schoolId,
                                       @Param("subjectsId") Long subjectsId,
                                       @Param("teacherId") Long teacherId,
                                       @Param("status") Integer status
    );

    /**
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param subjectsId
     * @param teacherId
     * @param dateTimeNow
     * @param status 教师发放：3，学生发放：4
     * @param laterStatus 教师发放：4，学生发放：5
     * @return
     */
    @Insert({"<script>",
            "INSERT INTO book_grant_info_paper ( year_part,quarter_num,book_student_id, grant_id,head_img, `status`,created_id, created_at ) SELECT",
            "t1.year_part,",
            "t1.quarter_num,",
            "t1.id AS book_student_id,",
            "t1.teacher_id AS grade_teacher_id,",
            "#{imgOss} AS head_img,",
            "${laterStatus} AS STATUS,",
            "${adminId} AS created_id,",
            "#{dateTimeNow} AS created_at ",
            "FROM",
            "book_student_paper AS t1",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${yearPart}",
            "and t1.quarter_num = ${quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t1.teacher_id = ${teacherId}",
            "AND t1.school_id = ${schoolId}",
            "AND t1.subjects_id = ${subjectsId}",
            "</script>"})
    public Integer addDataBookGrantPaper(@Param("yearPart") Integer yearPart,
                                         @Param("quarterNum") Integer quarterNum,
                                         @Param("schoolId") Long schoolId,
                                         @Param("subjectsId") Long subjectsId,
                                         @Param("teacherId") Long teacherId,
                                         @Param("dateTimeNow") String dateTimeNow,
                                         @Param("status") Integer status,
                                         @Param("laterStatus") Integer laterStatus,
                                         @Param("imgOss") String imgOss,
                                         @Param("adminId") Long adminId
    );

    @Update({"<script>",
            "UPDATE book_student_paper AS t1",
            "SET t1.`status` = ${laterStatus} ",
            ",t1.`grant_id` = ${teacherId} ",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${yearPart}",
            "and t1.quarter_num = ${quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t1.teacher_id = ${teacherId}",
            "AND t1.school_id = ${schoolId}",
            "AND t1.subjects_id = ${subjectsId}",
            "</script>"})
    public Integer grantBookPaper(@Param("yearPart") Integer yearPart,
                                  @Param("quarterNum") Integer quarterNum,
                                  @Param("schoolId") Long schoolId,
                                  @Param("subjectsId") Long subjectsId,
                                  @Param("teacherId") Long teacherId,
                                  @Param("status") Integer status,
                                  @Param("laterStatus") Integer laterStatus);

    /**
     * 教师可发放
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param subjectsId
     * @param teacherId
     * @param status
     * @param type
     * @return
     */
    @Select({"<script>",
            "SELECT",
            " * ",
            "FROM",
            "(",
                "SELECT",
                " * ",
                "FROM",
                "(",
                    "SELECT",
                    "t01.book_text_id,",
                    //1学生用书，2教师用书
                    "t01.book_text_type,",
                    "t01.lesson_num,",
                    "t04.`name` AS book_text_name,",
                    "t01.subjects_id,",
                    "t01.year_class_id,",
                    "t03.id AS grade_id,",
                    "t03.`name` AS grade_name,",
                    "getSubjectsName(t01.subjects_id) AS subjects_name,",
                    "getCourseTypeName(t01.class_type_id) AS class_type_name,",
                    "getDictName(t01.year_class_id, 14) AS year_class_name,",
                    "t01.class_type_id,",
                    "t06.`name` AS student_name,",
                    "COUNT( t01.id ) AS count ",
                    "FROM",
                    "book_student AS t01",
                    "INNER JOIN student_grade AS t02 ON t01.year_part = t02.year_part ",
                    "AND t01.quarter_num = t02.quarter_num ",
                    "AND t01.school_id = t02.school_id ",
                    "AND t01.student_id = t02.student_id ",
                    "AND t01.subjects_id = t02.subjects_id ",
                    "AND t01.lesson_type = t02.lesson_type ",
                    "AND t01.year_class_id = t02.year_class_id",
                    "AND t02.is_last = 1 ",
                    "AND t02.reading_status IN ( 0, 1 ) ",
                    "AND t01.deleted = t02.deleted",
                    "INNER JOIN grade AS t03 ON t02.grade_id = t03.id ",
                    "AND t03.deleted = 0 AND t01.class_type_id = t03.course_type_id",
                    "INNER JOIN book_text AS t04 ON t01.book_text_id = t04.id ",
                    "LEFT JOIN book_grant_info AS t05 ON t01.id = t05.book_student_id AND t05.`status` = 4 ",
                    "LEFT JOIN student AS t06 ON t06.id = t01.student_id ",
                    "WHERE",
                    "t01.`status` = ${status} ",
                    "AND t01.year_part = ${yearPart}",
                    "AND t01.quarter_num = ${quarterNum}",
                    "AND t01.deleted = 0 ",
                    "AND t03.teacher_id = ${teacherId}",
                    "AND t01.school_id = ${schoolId}",
                    "AND t01.subjects_id = ${subjectsId}",
                    "AND t01.book_text_type = 1",//学生用书
                    "<if test='isDetails == 1'>",
                        "AND t01.lesson_num = ${lessonNum}",
                    "</if>",
                    "<when test='gradeId !=null and gradeId != &apos;&apos;'>",
                        "AND t01.grade_id IN ( ${gradeId} )",
                    "</when>",
                    "<when test='studentName !=null and studentName != &apos;&apos;'>",
                        "AND t06.`name` LIKE '%${studentName}%' ",
                    "</when>",
                    "<when test='bookTextId !=null and bookTextId != &apos;&apos;'>",
                        "AND t01.`book_text_id` = ${bookTextId} ",
                    "</when>",
                    "<when test='classTypeId !=null and classTypeId != &apos;&apos;'>",
                        "AND t01.`class_type_id` = ${classTypeId} ",
                    "</when>",
                    "<when test='yearClassId !=null and yearClassId != &apos;&apos;'>",
                        "AND t01.`year_class_id` = ${yearClassId} ",
                    "</when>",
                    "GROUP BY ",
                    "t01.subjects_id,",
                    "t01.class_type_id,",
                    "t01.year_class_id,",
                    "t01.book_text_id,",
                    //1学生用书，2教师用书
                    "t01.book_text_type,",
                    "t01.lesson_num ",
                    "<if test='type == 2'>",
                        ",t02.grade_id ",
                    "</if>",
                    "<if test='isDetails == 1'>",
                        ",t01.id ",
                    "</if>",
                ") AS t1 ",

                "UNION ALL",

                "SELECT",
                " * ",
                "FROM",
                "(",
                    "SELECT",
                    "t01.book_text_id,",
                    "t01.book_text_type,",
                    "t01.lesson_num,",
                    "t04.`name` AS book_text_name,",
                    "t01.subjects_id,",
                    "t01.year_class_id,",
                    "t03.id AS grade_id,",
                    "t03.`name` AS grade_name,",
                    "getSubjectsName ( t01.subjects_id ) AS subjects_name,",
                    "getCourseTypeName ( t01.class_type_id ) AS class_type_name,",
                    "getDictName ( t01.year_class_id, 14 ) AS year_class_name,",
                    "t01.class_type_id,",
                    "t06.`name` AS student_name,",
                    "COUNT( t01.id ) AS count ",
                    "FROM",
                    "book_student AS t01",
                    "INNER JOIN (",
                        "SELECT",
                        "sg01.*,",
                        "g01.teacher_id,",
                        "g01.course_type_id AS class_type_id ",
                        "FROM",
                        "student_grade AS sg01",
                        "INNER JOIN grade AS g01 ON sg01.grade_id = g01.id ",
                        "WHERE",
                        "sg01.year_part = ${yearPart} ",
                        "AND sg01.quarter_num =  ${quarterNum} ",
                        "AND g01.lesson_type = 1 ",
                        "AND g01.deleted = 0 ",
                        "AND sg01.is_last = 1 ",
                        "AND sg01.reading_status IN ( 0, 1 ) GROUP BY g01.teacher_id,g01.school_id,g01.year_class_id,g01.course_type_id,g01.subjects_id",
                    ") t02 ON t01.year_part = t02.year_part ",
                    "AND t01.quarter_num = t02.quarter_num ",
                    "AND t01.school_id = t02.school_id ",
                    "AND t01.teacher_id = t02.teacher_id",
                    "AND t01.subjects_id = t02.subjects_id ",
                    "AND t01.lesson_type = t02.lesson_type ",
                    "AND t01.year_class_id = t02.year_class_id ",
                    "AND t01.class_type_id = t02.class_type_id ",
                    "AND t01.deleted = t02.deleted",
                    "INNER JOIN grade AS t03 ON t01.grade_id = t03.id ",
                    "AND t03.deleted = 0",
                    "INNER JOIN book_text AS t04 ON t01.book_text_id = t04.id ",
                    "LEFT JOIN book_grant_info AS t05 ON t01.id = t05.book_student_id AND t05.`status` = 4 ",
                    "LEFT JOIN student AS t06 ON t06.id = t01.student_id ",
                    "WHERE",
                    "t01.`status` = ${status} ",
                    "and t01.year_part = ${yearPart}",
                    "and t01.quarter_num = ${quarterNum}",
                    "AND t01.deleted = 0 ",
                    "AND t03.teacher_id = ${teacherId}",
                    "AND t01.school_id = ${schoolId}",
                    "AND t01.subjects_id = ${subjectsId}",
                    "<if test='isDetails == 1'>",
                        "AND t01.lesson_num = ${lessonNum}",
                    "</if>",
                    //1学生用书，2教师用书
                    "AND t01.book_text_type = 2",
                    "<when test='gradeId !=null and gradeId != &apos;&apos;'>",
                        "AND t01.grade_id IN ( ${gradeId} )",
                    "</when>",
                    "<when test='studentName !=null and studentName != &apos;&apos;'>",
                        "AND t06.`name` LIKE '%${studentName}%' ",
                    "</when>",
                    "<when test='bookTextId !=null and bookTextId != &apos;&apos;'>",
                        "AND t01.`book_text_id` = ${bookTextId} ",
                    "</when>",
                    "<when test='classTypeId !=null and classTypeId != &apos;&apos;'>",
                        "AND t01.`class_type_id` = ${classTypeId} ",
                    "</when>",
                    "<when test='yearClassId !=null and yearClassId != &apos;&apos;'>",
                        "AND t01.`year_class_id` = ${yearClassId} ",
                    "</when>",
                    "GROUP BY",
                    "t01.subjects_id,",
                    "t01.class_type_id,",
                    "t01.year_class_id,",
                    "t01.book_text_id,",
                    "t01.book_text_type, ",
                    "t01.lesson_num ",
                    "<if test='isDetails == 1'>",
                        ",t01.id ",
                    "</if>",
                ") AS t1 ",
            ") AS t where 1=1",
            "<when test='bookTextType !=null and bookTextType != &apos;&apos;'>",
                "AND book_text_type = ${bookTextType}",
            "</when>",
            "ORDER BY book_text_id,book_text_type,class_type_id,year_class_id",
            "<if test='type == 2'>",
                ",grade_id ",
            "</if>",
            "</script>"})
    public List<BookStudent> getDistributable(@Param("yearPart") Integer yearPart,
                                              @Param("quarterNum") Integer quarterNum,
                                              @Param("schoolId") Long schoolId,
                                              @Param("subjectsId") Long subjectsId,
                                              @Param("teacherId") Long teacherId,
                                              @Param("status") Integer status,
                                              @Param("type") Integer type,
                                              @Param("bookTextType") String bookTextType,
                                              @Param("isDetails") Integer isDetails,
                                              @Param("studentName") String studentName,
                                              @Param("gradeId") String gradeId,
                                              @Param("bookTextId") String bookTextId,
                                              @Param("classTypeId") Long classTypeId,
                                              @Param("yearClassId") String yearClassId,
                                              @Param("lessonNum") Integer lessonNum
    );

    /**
     * 学生可发放
     *
     * @param yearPart
     * @param quarterNum
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param status
     * @return
     */
    @Select({"<script>",
            "SELECT",
            "t01.book_text_id,",
            "t01.book_text_type,",
            "t01.lesson_num,",
            "t04.`name` AS book_text_name,",
            "t01.subjects_id,",
            "t01.year_class_id,",
            "t01.`status`,",
            "t01.student_id,",
            "t03.id AS grade_id,",
            "t03.`name` AS grade_name,",
            "getSubjectsName(t01.subjects_id) AS subjects_name,",
            "getCourseTypeName(t01.class_type_id) AS class_type_name,",
            "getDictName(t01.year_class_id, 14) AS year_class_name,",
            "t01.class_type_id",
            "FROM",
            "book_student AS t01",
            "INNER JOIN student_grade AS t02 ON t01.year_part = t02.year_part ",
            "AND t01.quarter_num = t02.quarter_num ",
            "AND t01.school_id = t02.school_id ",
            "AND t01.student_id = t02.student_id ",
            "AND t01.subjects_id = t02.subjects_id ",
            "AND t01.year_class_id = t02.year_class_id ",
            "AND t01.lesson_type = t02.lesson_type ",
            "AND t02.is_last = 1 ",
            "AND t02.reading_status IN ( 0, 1 ) ",
            "AND t01.deleted = t02.deleted",
            "INNER JOIN grade AS t03 ON t02.grade_id = t03.id ",
            "AND t03.deleted = 0 ",
            "INNER JOIN book_text AS t04 ON t01.book_text_id = t04.id ",
            "WHERE 1=1",
//            "t01.`status` &gt;= ${status} ",// 学生显示全部数据
            "and t01.year_part = ${yearPart}",
            "and t01.quarter_num = ${quarterNum}",
            "AND t01.deleted = 0 ",
            "AND t01.student_id = ${studentId}",
            "AND t01.school_id = ${schoolId}",
            "AND t01.book_text_type = ${bookTextType}",
            "<when test='subjectsId!=null and subjectsId!=&apos;&apos;'>",
                "AND t01.subjects_id = ${subjectsId} ",
            "</when>",
            "ORDER BY CASE t01.`status` WHEN 4 THEN 0 ELSE 1 END, t01.`status` ",// 可发放排在前面
            "</script>"})
    public List<BookStudent> getDistributableStudent(@Param("yearPart") Integer yearPart,
                                                     @Param("quarterNum") Integer quarterNum,
                                                     @Param("schoolId") Long schoolId,
                                                     @Param("subjectsId") Long subjectsId,
                                                     @Param("studentId") Long studentId,
                                                     @Param("status") Integer status,
                                                     @Param("bookTextType") Integer bookTextType
    );


    /**
     *
     * 学生发放
     *
     * @param yearPart
     * @param quarterNum
     * @param dateTimeNow
     * @param status 教师发放：3，学生发放：4
     * @param laterStatus 教师发放：4，学生发放：5
     * @return
     */
    @Insert({"<script>",
            "INSERT INTO book_grant_info ( year_part,quarter_num,book_student_id, grant_id,head_img, `status`,created_id, created_at ) SELECT",
            "t1.year_part,",
            "t1.quarter_num,",
            "t1.id AS book_student_id,",
            "t1.student_id AS grant_id,",
            "#{imgOss} AS head_img,",
            "${laterStatus} AS STATUS,",
            "${adminId} AS created_id,",
            "#{dateTimeNow} AS created_at ",
            "FROM",
            "book_student AS t1",
            "INNER JOIN student_grade AS t2 ON t1.year_part = t2.year_part ",
            "AND t1.quarter_num = t2.quarter_num ",
            "AND t1.school_id = t2.school_id ",
            "AND t1.student_id = t2.student_id ",
            "AND t1.subjects_id = t2.subjects_id ",
            "AND t1.year_class_id = t2.year_class_id ",
            "AND t1.lesson_type = t2.lesson_type ",
            "AND t2.is_last = 1 ",
            "AND t2.reading_status IN ( 0, 1 ) ",
            "AND t1.deleted = t2.deleted",
            "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
            "AND t3.deleted = t1.deleted",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${bookGrantRs.yearPart}",
            "and t1.quarter_num = ${bookGrantRs.quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t1.student_id = ${bookGrantRs.studentId}",
            "AND t1.school_id = ${bookGrantRs.schoolId}",
            //"AND t1.book_text_type = 1",
            //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
            "AND ",
            "<foreach collection='bookGrantRs.bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
                "( ",
                "t1.year_class_id = ${bookStudent.yearClassId}",
                "AND t1.class_type_id = ${bookStudent.classTypeId} ",
                "AND t1.subjects_id = ${bookStudent.subjectsId} ",
                "AND t1.book_text_id = ${bookStudent.bookTextId} ",
                // 1学生用书，2教师用书
                "AND t1.book_text_type = ${bookStudent.bookTextType} ",
                "<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
                    "AND t2.grade_id = ${bookStudent.gradeId} ",
                "</when>",
                "<when test='bookStudent.lessonNum !=null and bookStudent.lessonNum != &apos;&apos;'>",
                    "AND t1.lesson_num = ${bookStudent.lessonNum} ",
                "</when>",
                ") ",
            "</foreach>",
            "</script>"})
    public Integer addDataBookGrantStudent(@Param("yearPart") Integer yearPart,
                                           @Param("quarterNum") Integer quarterNum,
                                           @Param("adminId") Long adminId,
                                           @Param("dateTimeNow") String dateTimeNow,
                                           @Param("status") Integer status,
                                           @Param("laterStatus") Integer laterStatus,
                                           @Param("imgOss") String imgOss,
                                           @Param("bookGrantRs") BookGrantRs bookGrantRs
    );

    /**
     * 学生发放书本
     *
     * @param yearPart
     * @param quarterNum
     * @param status      教师发放：3，学生发放：4
     * @param laterStatus 教师发放：4，学生发放：5
     * @return
     */
    @Update({"<script>",
            "UPDATE book_student AS t1",
            "INNER JOIN student_grade AS t2 ON t1.year_part = t2.year_part ",
            "AND t1.quarter_num = t2.quarter_num ",
            "AND t1.school_id = t2.school_id ",
            "AND t1.student_id = t2.student_id ",
            "AND t1.subjects_id = t2.subjects_id ",
            "AND t1.year_class_id = t2.year_class_id ",
            "AND t1.lesson_type = t2.lesson_type ",
            "AND t2.is_last = 1 ",
            "AND t2.reading_status IN ( 0, 1 ) ",
            "AND t1.deleted = t2.deleted",
            "INNER JOIN grade AS t3 ON t2.grade_id = t3.id ",
            "AND t3.deleted = t1.deleted ",
            "SET t1.`status` = ${laterStatus} ",
            "WHERE",
            "t1.`status` = ${status} ",
            "and t1.year_part = ${bookGrantRs.yearPart}",
            "and t1.quarter_num = ${bookGrantRs.quarterNum}",
            "AND t1.deleted = 0 ",
            "AND t1.student_id = ${bookGrantRs.studentId}",
            "AND t1.school_id = ${bookGrantRs.schoolId}",
            //"AND t1.book_text_type = 1",
            //"AND t1.subjects_id = ${bookGrantRs.subjectsId}",
            "AND ",
            "<foreach collection='bookGrantRs.bookGrantInfoRsList' item='bookStudent' open='(' separator='OR' close=')'>",
                "( ",
                "t1.year_class_id = ${bookStudent.yearClassId}",
                "AND t1.class_type_id = ${bookStudent.classTypeId} ",
                "AND t1.book_text_id = ${bookStudent.bookTextId} ",
                // 1学生用书，2教师用书
                "AND t1.book_text_type = ${bookStudent.bookTextType} ",
                "AND t1.subjects_id = ${bookStudent.subjectsId} ",
                "<when test='bookStudent.gradeId !=null and bookStudent.gradeId != &apos;&apos;'>",
                    "AND t2.grade_id = ${bookStudent.gradeId} ",
                "</when>",
                "<when test='bookStudent.lessonNum !=null and bookStudent.lessonNum != &apos;&apos;'>",
                    "AND t1.lesson_num = ${bookStudent.lessonNum} ",
                "</when>",
                ") ",
            "</foreach>",
            "</script>"})
    public Integer grantBookStudent(@Param("yearPart") Integer yearPart,
                                    @Param("quarterNum") Integer quarterNum,
                                    @Param("adminId") Long adminId,
                                    @Param("status") Integer status,
                                    @Param("laterStatus") Integer laterStatus,
                                    @Param("bookGrantRs") BookGrantRs bookGrantRs
    );

}
